package cn.scau.info.chao;

import java.awt.Graphics;
import java.awt.Toolkit;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.swing.JFrame;
import javax.swing.UIManager;

public class DrawAxis extends JFrame {
	private Axis axis;
	private String text="最优路径长度： 102132";
	public DrawAxis() {
		try{
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		}catch(Exception e){
			
		}
	}
	
	/**
	 * @return the text
	 */
	public String getText() {
		return text;
	}

	/**
	 * @param text the text to set
	 */
	public void setText(String text) {
		this.text = text;
	}

	/**
	 * @return the axis
	 */
	public Axis getAxis() {
		return axis;
	}

	/**
	 * @param axis
	 *            the axis to set
	 */
	public void setAxis(Axis axis) {
		this.axis = axis;
	}

	public void paint(Graphics g) {

		super.paint( g);
		int XtickX = axis.getTickX();
		int XtickY = axis.getTickY();
		int XtickLength = axis.getTickLength();
		int XtickStep = axis.getTickStep();
		int tickHeight = 5;

		// 画横轴
		g.drawLine(XtickX, XtickY, XtickX + XtickLength, XtickY);
		for (int i = 0; i < XtickLength / XtickStep; i++) {
			g.drawLine(XtickX + i * XtickStep, XtickY, XtickX + i * XtickStep,
					XtickY - tickHeight);
		}

		// 画纵轴
		g.drawLine(XtickX, XtickY, XtickX, XtickY - XtickLength);
		for (int i = 0; i < XtickLength / XtickStep; i++) {
			g.drawLine(XtickX, XtickY - i * XtickStep, XtickX + tickHeight,
					XtickY - i * XtickStep);
		}

		// int[] x = new int[] { 82, 154, 293, 101, 363, 182 };
		// int[] y = new int[] { 52, 154, 193, 151, 343, 132 };
		//
		// g.drawPolyline(x, y, x.length);
		// 画点

		int cycleW = 5;
		int cycleH = 5;
		List<Coordinate> coordinates = axis.getCoordinates();
		for (Iterator iterator = coordinates.iterator(); iterator.hasNext();) {
			Coordinate coordinate = (Coordinate) iterator.next();

			g.fillOval(XtickX - cycleW / 2 + (int) coordinate.getX()
					* XtickStep, XtickY - cycleH / 2 - (int) coordinate.getY()
					* XtickStep, cycleW, cycleH);
		}

		List<Line> lines = axis.getLines();
		for (Iterator iterator = lines.iterator(); iterator.hasNext();) {
			Line line = (Line) iterator.next();
			g.drawLine(XtickX + (int) line.getA().getX() * XtickStep, XtickY
					- (int) line.getA().getY() * XtickStep, XtickX
					+ (int) line.getB().getX() * XtickStep, XtickY
					- (int) line.getB().getY() * XtickStep);
		}
		

		// 画原点坐标
		g.drawString(text, XtickX - 5, XtickY + 30);
		
		// 画原点坐标
		g.drawString("0", XtickX - 5, XtickY + 15);

		// 画横轴坐标
		for (int i = 1; i < XtickLength / XtickStep; i++) {
			g.drawString("" + (i), XtickX + i * XtickStep, XtickY + 15);
		}

		// 画横轴坐标
		for (int i = 1; i < XtickLength / XtickStep; i++) {
			g.drawString("" + (i), XtickX - 15, XtickY - i * XtickStep);
		}
	}

	public static void main(String[] args) {
		int width = 600;
		int height = 500;
		int len = 400;
		int step = 20;
		int tickX = (width - len) / 2;
		int tickY = (height + len) / 2;

		DrawAxis test = new DrawAxis();
		// 由外部用户给出参数
		Axis axisX = new Axis();
		axisX.setTickX(tickX);
		axisX.setTickY(tickY);
		axisX.setTickLength(len);
		axisX.setTickStep(step);

		List<Coordinate> coordinates = new ArrayList<Coordinate>();
		coordinates.add(new Coordinate(0, 0));
		coordinates.add(new Coordinate(1, 1));
		coordinates.add(new Coordinate(2, 2));
		coordinates.add(new Coordinate(5, 5));
		axisX.setCoordinates(coordinates);

		List<Line> lines = new ArrayList<Line>();
		lines.add(new Line(new Coordinate(1, 1), new Coordinate(2, 1)));
		lines.add(new Line(new Coordinate(1, 1), new Coordinate(2, 6)));
		axisX.setLines(lines);

		test.setAxis(axisX);

		test.setSize(width, height);
		test.setLocationRelativeTo(null);
		test.setVisible(true);
	}
}